package com.topisv.tms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.Permission;
import com.topisv.tms.entity.QueryParamHolder;
import com.topisv.tms.entity.Roleperm;
import com.topisv.tms.repository.PermissionDao;
import com.topisv.tms.repository.RolepermDao;
import com.topisv.tms.service.base.BaseService;

//Spring Bean的标识.
@Component
// 默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class PermissionService extends BaseService{

	private PermissionDao permissionDao;
	
	private RolepermDao rolepermDao;

	public Permission getPermission(Long id) {
		return permissionDao.findOne(id);
	}

	@Transactional(readOnly = false)
	public void savePermission(Permission entity) {
		permissionDao.save(entity);
	}
	
	@Transactional(readOnly = false)
	public void saveRoleperm(Roleperm entity) {
		rolepermDao.save(entity);
	}
	
	@Transactional(readOnly = false)
	public void deleteRoleperm(Long roleId) {
		rolepermDao.deleteByRoleId(roleId);
	}
	
	public List<Permission> findPermByRoleId(Long roleId)
	{
		return rolepermDao.findByRoleId(roleId);
	}
	public List<Permission> findAppPermByRoleId(Long roleId)
	{
		return rolepermDao.findAppByRoleId(roleId);
	}

	@Transactional(readOnly = false)
	public void deletePermission(Long id) {
		permissionDao.delete(id);
	}

	public List<Permission> getAllPermission() {
		return (List<Permission>) permissionDao.findAll();
	}
	
	public List<Permission> findByFunctionId(Long functionid) {
		return (List<Permission>) permissionDao.findByFunctionId(functionid);
	}

	public Page<Permission> getPermission(QueryParamHolder paramHolder) {
		return findByJson(paramHolder, permissionDao, Permission.class);
	}

	@Autowired
	public void setPermissionDao(PermissionDao permissionDao) {
		this.permissionDao = permissionDao;
	}
	@Autowired
	public void setRolepermDao(RolepermDao rolepermDao) {
		this.rolepermDao = rolepermDao;
	}
	
	
}
